package Dao.Process.Imp;

import Dao.Process.AdviserGradeDao;
import Entity.Grade.AdviserGrade;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdviserGradeDaoImp implements AdviserGradeDao {
    private PreparedStatement ps;
    private ResultSet rs;

    @Override
    public AdviserGrade getAdviserGradeBySNO(String SNO, Connection conn) {
        AdviserGrade adviserGrade = null;
        try {
            String sql = "select " +
                    "id, sno, tno, assignment, report, text_part, paper_word, abstract, check_accept, is_truant, " +
                    "truant_days, is_reply, remark, grade1, grade2, grade3, grade4, grade5, grade6, grade_state, pass_state " +
                    "from adviser_grade " +
                    "where sno = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, SNO);
            rs = ps.executeQuery();
            if (rs.next()) {
                Integer id = rs.getInt(1);
                String sno = rs.getString(2);
                String tno = rs.getString(3);
                String assignment = rs.getString(4);
                String report = rs.getString(5);
                String textPart = rs.getString(6);
                Integer paperWord = rs.getInt(7);
                String abstract1 = rs.getString(8);
                String checkAccept = rs.getString(9);
                String isTruant = rs.getString(10);
                Integer truantDays = rs.getInt(11);
                String isReply = rs.getString(12);
                String remark = rs.getString(13);
                Integer grade1 = rs.getInt(14);
                Integer grade2 = rs.getInt(15);
                Integer grade3 = rs.getInt(16);
                Integer grade4 = rs.getInt(17);
                Integer grade5 = rs.getInt(18);
                Integer grade6 = rs.getInt(19);
                String gradeState = rs.getString(20);
                String passState = rs.getString(21);
                adviserGrade
                        = new AdviserGrade(id, sno, tno, assignment, report, textPart, paperWord, abstract1, checkAccept,
                        isTruant, truantDays, isReply, remark, grade1, grade2, grade3, grade4, grade5, grade6, gradeState, passState);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return adviserGrade;
    }

    @Override
    public Integer addAdviserGrade(AdviserGrade grade, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into adviser_grade" +
                    "(sno, tno, assignment, report, text_part, paper_word, abstract, check_accept, is_truant, truant_days, is_reply, " +
                    "remark, grade1, grade2, grade3, grade4, grade5, grade6, grade_state, pass_state)" +
                    " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, grade.getSno());
            ps.setObject(2, grade.getTno());
            ps.setObject(3, grade.getAssignment());
            ps.setObject(4, grade.getReport());
            ps.setObject(5, grade.getTextPart());
            ps.setObject(6, grade.getPaperWord());
            ps.setObject(7, grade.getAbstract1());
            ps.setObject(8, grade.getCheckAccept());
            ps.setObject(9, grade.getIsTruant());
            ps.setObject(10, grade.getTruantDays());
            ps.setObject(11, grade.getIsReply());
            ps.setObject(12, grade.getRemark());
            ps.setObject(13, grade.getGrade1());
            ps.setObject(14, grade.getGrade2());
            ps.setObject(15, grade.getGrade3());
            ps.setObject(16, grade.getGrade4());
            ps.setObject(17, grade.getGrade5());
            ps.setObject(18, grade.getGrade6());
            ps.setObject(19, grade.getGradeState());
            ps.setObject(20, grade.getPassState());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
}
